package com.atguigu.tingshu.config;

import com.atguigu.tingshu.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: AccountRabbitConfig
 * Package: com.atguigu.tingshu.user.config
 * Description:初始化账户的rabbitmq的配置
 */
@Configuration
public class AccountRabbitConfig {

    /**
     * 实例化交换机
     * @return
     */
    @Bean("accountExchange")
    public Exchange accountExchange(){
        return ExchangeBuilder.directExchange(RabbitConstant.ACCOUNT_INIT_EXCHANGE).build();
    }

    /**
     * 实例化队列
     * @return
     */
    @Bean("accountQueue")
    public Queue accountQueue(){
        return QueueBuilder.durable(RabbitConstant.ACCOUNT_INIT_QUEUE).build();
    }

    /**
     * 初始化队列和交换机的绑定关系
     * @param accountExchange
     * @param accountQueue
     * @return
     */
    @Bean
    public Binding queueBinding(@Qualifier("accountExchange") Exchange accountExchange,
                                @Qualifier("accountQueue")Queue accountQueue){
        return BindingBuilder.bind(accountQueue).to(accountExchange).with(RabbitConstant.ACCOUNT_INIT_ROUTING).noargs();
    }
}
